<template>
  <!-- 唯一一个根元素 -->
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="../../assets/img/login_icon.png" alt="" /><span class="title"
          >黑马面面</span
        ><span class="line"></span><span class="sub-title">用户登录</span>
      </div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="login-form"
      >
        <el-form-item prop="phone">
          <el-input
            v-model="ruleForm.phone"
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            v-model="ruleForm.password"
            show-password
          >
          </el-input>
        </el-form-item>
        <el-row>
          <el-col :span="16">
            <el-form-item prop="code">
              <el-input
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
                v-model="ruleForm.code"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7"
            ><img
              style="width:100%;height:40px;"
              @click="changeCodeURL"
              :src="loginCodeURL"
              alt=""
          /></el-col>
        </el-row>
        <el-form-item prop="checked">
          <el-checkbox v-model="ruleForm.checked"></el-checkbox
          >&nbsp;我已阅读并同意
          <el-link type="primary" href="http://www.itcast.cn" target="_blank"
            >用户协议</el-link
          >和<el-link type="primary" href="http://www.itcast.cn" target="_blank"
            >隐私条款</el-link
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="register"
            >注册</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="right"><img src="../../assets/img/login_bg.png" alt="" /></div>
    <!-- 注册子组件 -->
    <register ref="registerRef" />
  </div>
</template>

<script>
// 按需引入
import { setToken } from '@/utils/token'
// 引入注册弹窗组件
import Register from './Register'
export default {
  name: 'Login',
  components: {
    Register
  },
  data () {
    return {
      // 请求验证码地址
      loginCodeURL: 'http://127.0.0.1/heimamm/public/captcha?type=login',
      ruleForm: {
        phone: '18511111111', //手机号
        password: '12345678', //密码
        code: '', //验证码具有不确定性
        checked: false //默认复选框为false
      },
      //   校验
      rules: {
        phone: [
          {
            // validator 是校验器
            validator: (rule, value, callback) => {
              // rule 检验对象、value 输入的值 callback 校验成功还是失败的【一定要调用】
              if (!value) {
                return callback(new Error('手机号不能为空'))
              }

              // 手机的正则校验
              const reg = /^1[3-9][0-9]{9}$/
              if (!reg.test(value)) {
                return callback(new Error('手机号不合法'))
              }

              // 校验成功不需要加任何参数，但是一定要调用
              callback()
            },
            trigger: 'blur' // 触发时机
          }
        ],
        password: [
          {
            required: true, // 非空
            message: '密码不能为空', // 提示文字
            trigger: 'blur' // 触发时机
          },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ],
        code: [
          {
            required: true, // 非空
            message: '验证码不能为空', // 提示文字
            trigger: 'blur' // 触发时机
          }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              value ? callback() : callback(new Error('必须勾选用户协议'))
            },
            trigger: 'change' // 触发时机
          }
        ]
      }
    }
  },
  methods: {
    //   验证码根据点击次数发送请求路径 随机生成是根据毫秒数 没有重复的可能
    changeCodeURL () {
      this.loginCodeURL = `${
        process.env.VUE_APP_BASEURL
      }/captcha?type=login&t=${new Date() - 0}`
    },
    // 登录
    login () {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return

        const res = await this.$axios.post('/login', this.ruleForm)
        // console.log('2-----', res)

        if (res.code === 200) {
          this.$message.success('登录成功！')

          // 保存token
          // localStorage.setItem('heima62_token', res.data.token)
          setToken(res.data.token)

          //   跳转页面
          this.$router.push('/layout')
        } else {
          this.$message.error(res.data.message)

          // 重新获取验证码
          this.changeCodeURL()
        }
      })
    },
    register () {
      this.$refs.registerRef.dialogVisible = true;
      this.$nextTick(() => {
        // console.log(this.$refs.registerRef.$refs.form)
      })
    }
  }
}
</script>

<style lang="less">
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .login-form {
      margin-top: 29px;
    }
  }
}
</style>
